package com.photoeditor.demo.ui.widget;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.photoeditor.R;
import com.photoeditor.demo.app.BaseRecycleViewAdapter;
import com.photoeditor.demo.app.ItemClickSupport;

/**
 * Created by winn on 17/5/9.
 */

public class HRecyclerView extends RecyclerView {

    private ItemClickSupport mOnItemClickListener;

    private ViewGroup mSlideableParent;
    private boolean mIsScrollToFirstItem;
    private float mLastX;

    public HRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public HRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HRecyclerView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context ctx) {
        setOrientation(false);
    }

    /**
     * 设定方向，初始化后必须被调用
     *
     * @return void
     * @throw
     */
    public void setOrientation(final boolean isV) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        int orientation = isV ? LinearLayoutManager.VERTICAL : LinearLayoutManager.HORIZONTAL;
        linearLayoutManager.setOrientation(orientation);
        setLayoutManager(linearLayoutManager);
    }

    /**
     * 设置itemView的额外间隔
     *
     * @return void
     * @throw
     */
    public void setSpacing(final int spacing) {
        addItemDecoration(new ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
                int pos = parent.getChildPosition(view);
                int spacingVal = spacing;

                Adapter adapter = getAdapter();
                if (pos == adapter.getItemCount() - 1) {
                    spacingVal = 0;
                }
                outRect.set(0, 0, spacingVal, 0);
            }
        });
    }

    public void setMargin(final int marginLeft, final int marginTop, final int marginRight, final int marginBottom) {
        addItemDecoration(new ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
                int pos = parent.getChildPosition(view);

                Adapter adapter = getAdapter();
                if (pos == 0) {
                    outRect.set(marginLeft, 0, 0, 0);
                } else if (pos == adapter.getItemCount() - 1) {
                    outRect.set(0, 0, marginRight, 0);
                }

            }
        });
    }

    /**
     * 同ListView的setSelection
     *
     * @return void
     * @throw
     */
    public void setSelection(int position) {
        smoothScrollToPosition(position);
    }

    /**
     * 显示到指定position，没有滑动动画
     *
     * @param position
     */
    public void setSelectionNoAni(int position) {
        scrollToPosition(position);
    }

    /**
     * 设定item点击相应
     *
     * @return void
     * @throw
     */
    public void setOnItemClickListener(ItemClickSupport l) {
        BaseRecycleViewAdapter adapter = (BaseRecycleViewAdapter) getAdapter();
        if (adapter == null) {
            mOnItemClickListener = l;
        } else {
            adapter.setItemClickSupport(l);
        }
    }

    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);
        if (mOnItemClickListener != null) {
            BaseRecycleViewAdapter tmpAdapter = (BaseRecycleViewAdapter) adapter;
            tmpAdapter.setItemClickSupport(mOnItemClickListener);
            mOnItemClickListener = null;
        } else {
        }
    }

    public void setScrollToFirstItem(boolean scrollToFirst) {
        mIsScrollToFirstItem = scrollToFirst;
    }

    public boolean isScrollToFirstItem() {
        return mIsScrollToFirstItem;
    }

    public void setSlideableParent(ViewGroup slideableParent) {
        mSlideableParent = slideableParent;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //修复CameraFragment的filter二级栏和模糊预览界面的滑动冲突
        if ((getId() == R.id.filter_category_list || getId() == R.id.filter_listview) && mSlideableParent != null) {
            int action = ev.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    mSlideableParent.requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_MOVE:
                    float dx = ev.getX() - mLastX;
                    if (dx < 0) {
                        //向右滑，不拦截
                        mSlideableParent.requestDisallowInterceptTouchEvent(true);
                    } else {
                        if (isScrollToFirstItem()) {
                            mSlideableParent.requestDisallowInterceptTouchEvent(false);
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    break;
                default:
                    break;
            }
            mLastX = ev.getX();
        }
        return super.dispatchTouchEvent(ev);
    }
}
